﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XXX.Common
{
    public class ExLog
    {
        public static bool WriteTxt(string str)
        {
            try
            {

                string date = DateTime.Now.ToString("yyyyMMdd");
                //string path = HttpContext.Current.Server.MapPath("~/log/log" + date + ".txt");
                string path = AppDomain.CurrentDomain.BaseDirectory + "log/log" + date + ".txt";

                FileStream fs = new FileStream(path, FileMode.Append);
                StreamWriter sw = new StreamWriter(fs);
                //开始写入
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + ":" + str);
                //清空缓冲区
                sw.Flush();
                //关闭流
                sw.Close();
                fs.Close();
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

        public static bool WriteTxt(string FileName, string str)
        {
            try
            {

                string date = DateTime.Now.ToString("yyyyMMdd");
                //string path = HttpContext.Current.Server.MapPath("~/log/log" + date + ".txt");
                string path = AppDomain.CurrentDomain.BaseDirectory + "log/" + FileName + date + ".txt";

                FileStream fs = new FileStream(path, FileMode.Append);
                StreamWriter sw = new StreamWriter(fs);
                //开始写入
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + ":" + str);
                //清空缓冲区
                sw.Flush();
                //关闭流
                sw.Close();
                fs.Close();
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

        public static bool WriteTxt_JK(string str)
        {
            try
            {

                string date = DateTime.Now.ToString("yyyyMMdd");
                //string path = HttpContext.Current.Server.MapPath("~/log/log" + date + ".txt");
                string path = AppDomain.CurrentDomain.BaseDirectory + "log/log_JK" + date + ".txt";

                FileStream fs = new FileStream(path, FileMode.Append);
                StreamWriter sw = new StreamWriter(fs);
                //开始写入
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + ":" + str);
                //清空缓冲区
                sw.Flush();
                //关闭流
                sw.Close();
                fs.Close();
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

        public static bool WriteException(string str)
        {
            try
            {

                string date = DateTime.Now.ToString("yyyyMMdd");
                //string path = HttpContext.Current.Server.MapPath("~/log/log" + date + ".txt");
                string path = AppDomain.CurrentDomain.BaseDirectory + "log/Exception" + date + ".txt";

                FileStream fs = new FileStream(path, FileMode.Append);
                StreamWriter sw = new StreamWriter(fs);
                //开始写入
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + ":" + str);
                //清空缓冲区
                sw.Flush();
                //关闭流
                sw.Close();
                fs.Close();
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
    }
}
